package com.lei.sqlGoodsJsp.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lei.sqlGoodsJsp.dao.Impl.JDBC;
import com.lei.sqlGoodsJsp.entity.User;
import com.lei.sqlGoodsJsp.service.UserLoginService;
import com.lei.sqlGoodsJsp.service.Impl.UserLoginServiceImpl;

public class VerifyServletLogin extends HttpServlet {
	Connection conn = JDBC.getConn();
	List<User> list = new ArrayList<User>();
	static UserLoginService userLoginService = new UserLoginServiceImpl();

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String userName = req.getParameter("userName");
		String password = req.getParameter("password");
		String verifyCode = req.getParameter("verifyCode");
		String sessionVerifyCode = (String) req.getSession().getAttribute("verify");
		// 校验验证码
		if (sessionVerifyCode != null && !sessionVerifyCode.equals(verifyCode)) {
			req.setAttribute("msg", "验证码错误");
			req.getRequestDispatcher("sql/verify.jsp").forward(req, resp);
			return;
		}
		// 校验用户名密码
		for (User user : list) {
			if (user.equals(userName) && user.equals(password)) {
				req.getSession().setAttribute("loginInfo", "abc");
				resp.sendRedirect("GoodsPageServlet?method=list");
			} else {
				req.setAttribute("msg", "账号或密码错误");
				req.getRequestDispatcher("sql/verify.jsp").forward(req, resp);
			}
		}
	}
}
